<?php

declare (strict_types = 1);

namespace app\command;

use app\model\User as UserModel;
use think\console\Input;
use think\console\Output;

class Song extends BaseCommand
{
    protected function configure()
    {
        // 指令配置
        $this->setName('Song')
            ->setDescription('StartAdmin Test Command');
    }

    protected function execute(Input $input, Output $output)
    {
        // cache('SongList', null);
        // cache('SongNow', null);
        // cache('SongLast', null);
        // return;
        $userModel = new UserModel();
        for ($i = 0; $i < 60; $i++) {
            sleep(1);
            $songList = cache('SongList') ?? '';
            $songList = json_decode($songList, true) ?? [];
            $now = cache('SongNow') ?? '';
            if ($now) {
                //当前有歌正在播放
                continue;
            }
            //上次播放的音乐已超时 去队列取出第一个
            if (count($songList) > 0) {
                $now = $songList[0];
                array_shift($songList);
                cache('SongList', json_encode($songList), 86400);
                cache('SongNow', $now, 600);
            }
            if (!$now) {
                //实在是没人点歌了 去随机一个
                try {
                    $url = 'https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg?type=1&json=1&utf8=1&onlysong=0&new_format=1&disstid=7678211243&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq.json&needNewCode=0';
                    $res = curlHelper($url, 'GET', null, [
                        'referer: https://y.qq.com/portal/player.html',
                    ])['body'];
                    $arr = json_decode($res, true);
                    $songList = $arr['cdlist'][0]['songlist'];
                    $song = $songList[rand(0, count($songList) - 1)];
                    $now = [
                        'name' => $song['name'],
                        'id' => $song['id'],
                        'mid' => $song['mid'],
                        'user' => 1,
                    ];
                    //查询一下歌曲是否可播放
                    $res = curlHelper("https://u.y.qq.com/cgi-bin/musicu.fcg?format=json&data=%7B%22req_0%22%3A%7B%22module%22%3A%22vkey.GetVkeyServer%22%2C%22method%22%3A%22CgiGetVkey%22%2C%22param%22%3A%7B%22guid%22%3A%22358840384%22%2C%22songmid%22%3A%5B%22" . $now['mid'] . "%22%5D%2C%22songtype%22%3A%5B0%5D%2C%22uin%22%3A%221443481947%22%2C%22loginflag%22%3A1%2C%22platform%22%3A%2220%22%7D%7D%2C%22comm%22%3A%7B%22uin%22%3A%2218585073516%22%2C%22format%22%3A%22json%22%2C%22ct%22%3A24%2C%22cv%22%3A0%7D%7D");
                    $data = json_decode($res['body'], true);
                    if ($data['code'] == 0 && array_key_exists('req_0', $data) && array_key_exists('data', $data['req_0']) && array_key_exists('midurlinfo', $data['req_0']['data']) && array_key_exists('sip', $data['req_0']['data']) && array_key_exists('testfilewifi', $data['req_0']['data'])) {
                        $music = $data['req_0']['data'];
                        if (count($music['midurlinfo']) > 0 && array_key_exists('purl', $music['midurlinfo'][0])) {
                            if (!$music['midurlinfo'][0]['purl']) {
                                //无法播放
                                continue;
                            }
                        } else {
                            //无法播放
                            continue;
                        }
                    } else {
                        //无法播放
                        continue;
                    }
                    cache('SongNow', $now, 600);
                } catch (\Exception $e) {
                    continue;
                }
            }
            if (!$now) {
                continue;
            }
            $this->console("当前歌单" . count($songList) . "首 正在播放：" . $now['name']);
            //send
            $user = $userModel->where('user_id', $now['user'])->find();
            $user = $user ? $user->toArray() : [];
            if (!$user) {
                cache('SongNow', null);
            } else {
                $user = [
                    'user_id' => $user['user_id'],
                    'user_name' => $user['user_name'],
                    'user_plat' => $user['user_plat'],
                    'user_login' => $user['user_login'],
                    'user_head' => $user['user_head'],
                ];
                $msg = json_encode([
                    'song' => $now,
                    "user" => $user,
                    "type" => "now",
                    "time" => date('H:i:s'),
                ]);
                $ret = curlHelper("http://127.0.0.1:9999", "POST", http_build_query([
                    'token' => "dqwhohigoqwhoghqohgoqwhgioqowghqiowhgoqhwgoqhw",
                    'channel' => "bbbug_com",
                    'msg' => $msg,
                ]), [
                    'content-type:application/x-www-form-urlencode',
                ]);
            }
        }
    }
}
